import React from 'react';
import { Spin } from 'antd';
import { isEqual, isEmpty } from 'lodash';
import {WeaErrorPage, WeaTools, WeaTable, WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import Title from './Title';
import { formatData,setContentviewHeight } from '../../util/common';
//内容组件
class EContent extends React.Component {
    constructor(props) {
        super(props);
        const currentTab = props.edata.currenttab || props.edata.tabids[0];
        this.state = {
            tabdata: props.edata.data[currentTab],
            currentTab: currentTab,
            refresh: false,
        }
        this.handleTabData = this.handleTabData.bind(this);
    }
    componentWillReceiveProps(nextProps){
        if(!isEqual(nextProps, this.props)){
            const currentTab = nextProps.edata.currenttab || nextProps.edata.tabids[0];
            this.setState({
                tabdata: nextProps.edata.data[currentTab],
                currentTab: currentTab,
                refresh: false,
            }); 
        }
    }
    componentDidMount(){
        this.props.onRef(this);
    }
    shouldComponentUpdate(nextProps, nextState){
        return !isEqual(this.state, nextState) || !isEqual(this.props, nextProps)
    }
    handleTabData(tabid){
        const { edata, config } = this.props;
        const { params } = config;
        const { currentTab } = this.state;
        this.setState({
            currentTab: tabid,
            tabdata: edata.data[tabid],
            refresh: tabid === currentTab,
        });
        setTimeout(()=>{
            this.setState({
                refresh: false,
            });
        },100);
    }
    render() {
        const { config, edata, handleRefresh, handleDelete, fontSizeMp } = this.props;
        const { tabdata, currentTab, refresh } = this.state; 
        const { eid } = config.item;
        const { tabids, titles, esetting, counts } = edata;
        const { contentview = {},header } = config.item;
        let cstyle = {};
        if(header.displayPosition && header.displayPosition == 'bottom' && tabids && tabids.length > 1){
            if(contentview.style.height){
                if(esetting.tabstyle == '1' && esetting.tabvalue){
                    cstyle = setContentviewHeight(contentview.style.height,esetting.tabvalue)
                }else{
                    cstyle = {height: parseInt(contentview.style.height)-32+"px",overflow: "auto"};
                }   
            }else {
                cstyle = {height: contentview.style.height,overflow: "auto"};
            }
        }else if(tabids && tabids.length == 1){
            cstyle = {height: contentview.style.height,overflow: "auto"};
        }
        if(header.displayPosition && header.displayPosition == 'bottom') {
            if (tabids && tabids.length > 1 || tabids && tabids.length == 1 && esetting.isAlwaysShow == '1') {
                let elementH=parseInt(contentview.style.height)-parseInt($(`#titleContainer_${eid}`).css('height'))-33;
                if(elementH!=parseInt(cstyle.height)){
                    if(elementH<parseInt(cstyle.height)){
                        cstyle.height=elementH+"px";
                    }
                }
            }
        }
        let { fontStyle } = config.item;
        if(!isEmpty(fontSizeMp)) fontStyle = fontSizeMp;
        esetting.fontStyle = fontStyle;
        let contentHtml = tabdata && tabdata.length ? <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@5qt3si`} columns={formatData(tabdata[0], esetting)} showHeader={false} pagination={false} dataSource={tabdata} size="small"/>
                :
            <div className = "ant-table-placeholder">
            <span> <i className = "anticon anticon-frown"></i>{getLabel(83553, '暂无数据')}</span>
            </div>;
        const _titleProps = {
            currentTab,
            config,
            titles,
            tabids,
            counts,
            handleRefresh,
            handleDelete,
            fontSizeMp,
            tabwidth: esetting.tabwidth,
            methodvalue: esetting.methodvalue,
            tabstyle: esetting.tabstyle,
            tabvalue: esetting.tabvalue,
            handleTabData: this.handleTabData
        }
        return <div>
            <Title ecId={`${this && this.props && this.props.ecId || ''}_Title@lm5qco`} {..._titleProps}/>
            <div className="tabContant" id={`tabcontant_${eid}`} style={{height: cstyle.height,'overflow-y':'auto'}}>
                <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@v944vk`} spinning={refresh}>{contentHtml}</Spin>
            </div>
         </div>;
    }
}

class MyErrorHandler extends React.Component {
    render() {
        const hasErrorMsg = this.props.error && this.props.error !== "";
            return ( <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@j5ovvv`} msg = { hasErrorMsg ? this.props.error :getLabel(385218,"对不起，该页面异常，请联系管理员！")}/>
        );
    }
}
EContent = WeaTools.tryCatch(React, MyErrorHandler, { error: "" })(EContent);
export default EContent;
